﻿using System.Web;
using System.Web.Mvc;
using Actya.Core.SiteStructure;

namespace Actya.Web.Mvc.Helpers
{
	public static class NavigationExtensions
	{
		/// <summary>
		/// Render the main menu.
		/// </summary>
		/// <param name="cmsHelper"></param>
		/// <param name="defaultExpandLevel"></param>
		/// <param name="levelsToMergeWithRoot"></param>
		/// <returns></returns>
		public static HtmlString MainMenu(this CmsHelper cmsHelper, int defaultExpandLevel, int levelsToMergeWithRoot = 0)
		{
			return MainMenu(cmsHelper, defaultExpandLevel, null, levelsToMergeWithRoot);
		}

		/// <summary>
		/// Render the main menu.
		/// </summary>
		/// <param name="cmsHelper"></param>
		/// <param name="defaultExpandLevel"></param>
		/// <param name="maxExpandLevel"></param>
		/// <param name="levelsToMergeWithRoot"></param>
		/// <returns></returns>
		public static HtmlString MainMenu(this CmsHelper cmsHelper, int defaultExpandLevel, int? maxExpandLevel, int levelsToMergeWithRoot = 0)
		{
			var navigationHelper = new NavigationHelper(cmsHelper.CmsContext, DependencyResolver.Current.GetService<IPageService>());
			return navigationHelper.RenderMainMenu(defaultExpandLevel, maxExpandLevel, levelsToMergeWithRoot);
		}

		/// <summary>
		/// Render the context menu. The items in this menu are the children or the siblings of the current sitemap node
		/// and the given level.
		/// </summary>
		/// <param name="cmsHelper"></param>
		/// <param name="level"></param>
		/// <param name="levelsToExpand"></param>
		/// <returns></returns>
		public static HtmlString SubMenu(this CmsHelper cmsHelper, int level, int levelsToExpand = 0)
		{
			var navigationHelper = new NavigationHelper(cmsHelper.CmsContext, DependencyResolver.Current.GetService<IPageService>());
			return navigationHelper.RenderSubMenu(level, levelsToExpand);
		}

		/// <summary>
		/// Get the absolute url for the given page.
		/// </summary>
		/// <param name="cmsHelper"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static HtmlString PageUrl(this CmsHelper cmsHelper, Page page)
		{
			return AbsoluteUrl(cmsHelper, page.Url);
		}

		/// <summary>
		/// Get the absolute url for the given url.
		/// </summary>
		/// <param name="cmsHelper"></param>
		/// <param name="url"></param>
		/// <returns></returns>
		public static HtmlString AbsoluteUrl(this CmsHelper cmsHelper, string url)
		{
			var navigationHelper = new NavigationHelper(cmsHelper.CmsContext, DependencyResolver.Current.GetService<IPageService>());
			return MvcHtmlString.Create(navigationHelper.GetAbsoluteUrl(url));
		}
	}
}